rm(list = ls(all = TRUE))
#library(ggplot2)
#library(reshape2)

# DATA AND PATHS
# --------------
budgetFile <- "./original_data/budget_shares_with_ministers.csv"

# read budget data
data <- read.csv(budgetFile,stringsAsFactors=FALSE)
data$debate.year <- as.numeric(format(as.Date(data$debate.date), "%Y"))

# drop reference documents
data <- subset(data, !(data$portfolio %in% c("Taoiseach","Finance")))

# only keep one observation for each portfolio
data <- data[!duplicated(paste(data$year,data$portfolio)),]

# remove reform department, which was part of finance
data <- data[!data$portfolio=="Reform",]


# PLOT: CHANGES IN SHARES BY PORTFOLIO
# ------------------------------------
# only keep relevant columns
d <- data[,c("year","portfolio","change.current.share","change.capital.share")]

# drop 1999 because change not available
d <- d[d$year!=1999,]

# from long to wide
d <- melt(d, id=c("portfolio","year"))

# rename factor variable
levels(d$variable) <- c("Change current share","Change capital share")


p <- ggplot(d, aes(x=year, y=value)) +
    geom_line(aes(color=variable, lty=variable), cex=1.5) +
    theme_bw() +
    facet_wrap(~ portfolio, scales="free_y") + 
    xlab("Budget year") +
    ylab("Annual change in share of total budget (percentage points)") +
    labs(title="Change in budget shares by portfolio") +
    theme(legend.position = "bottom") +
    theme(text = element_text(size=14)) + 
    guides(
        color = guide_legend(title = "Legend:"),
        lty = guide_legend(title = "Legend:"))


pdf(file="./plots/figure2-change_budget_shares_by_portfolio.pdf", height=8, width=12)
print(p)
dev.off()


# PLOT: SHARES BY PORTFOLIO
# -------------------------
# only keep relevant columns
d <- data[,c("year","portfolio","current.share","capital.share")]

# from long to wide
d <- melt(d, id=c("portfolio","year"))

# rename factor variable
levels(d$variable) <- c("Current share","Capital share")

p <- ggplot(d, aes(x=year, y=value)) +
    geom_line(aes(color=variable, lty=variable), cex=1.5) +
    theme_bw() +
    facet_wrap(~ portfolio, scales="free_y") + 
    xlab("Budget year") +
    ylab("Share of total budget (%)") +
    labs(title="Budget shares by portfolio") +
    theme(legend.position = "bottom")  +
    theme(text = element_text(size=14)) + 
    guides(
        color = guide_legend(title = "Legend:"),
        lty = guide_legend(title = "Legend:"))


pdf(file="./plots/appendix_figure1.3-budget_shares_by_portfolio.pdf", height=8, width=12)
print(p)
dev.off()
